package com.lineage.server.command.executor;

import com.lineage.server.datatables.NpcSpawnTable;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.datatables.SpawnTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.utils.L1SpawnUtil;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class L1InsertSpawn implements L1CommandExecutor {
  private static Logger _log = Logger.getLogger(L1InsertSpawn.class.getName());
  
  public static L1CommandExecutor getInstance() {
    return new L1InsertSpawn();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    String msg = null;
    try {
      StringTokenizer tok = new StringTokenizer(arg);
      String type = tok.nextToken();
      int npcId = Integer.parseInt(tok.nextToken().trim());
      L1Npc template = NpcTable.get().getTemplate(npcId);
      if (template != null) {
        if (type.equalsIgnoreCase("mob")) {
          if (!template.getImpl().equals("L1Monster")) {
            msg = "指定的NPC不是L1Monster类型。";
            return;
          } 
          SpawnTable.storeSpawn(pc, template);
        } else if (type.equalsIgnoreCase("npc")) {
          NpcSpawnTable.get().storeSpawn(pc, template);
        } 
        L1SpawnUtil.spawn(pc, npcId, 0, 0);
        msg = String.valueOf(template.get_name()) + " (" + npcId + ") " + "新增到资料库中。";
      } 
      msg = "找不到符合条件的NPC。";
      return;
    } catch (Exception e) {
      _log.log(Level.SEVERE, "", e);
      msg = "请输入 : " + cmdName + " mob|npc NPCID 。";
      return;
    } finally {
      if (msg != null)
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(msg)); 
    } 
  }
}
